<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="设备ID" prop="device_id">
                    <!-- <el-select class="flex-1" v-model="formData.device_id" clearable placeholder="请选择设备ID">
                        <el-option v-for="(item, index) in dictData." :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select> -->
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                    <material-picker v-model="formData.icon" />
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="副标题" prop="subhead">
                    <el-input v-model="formData.subhead" clearable placeholder="请输入副标题" />
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入价格" />
                </el-form-item>
                <el-form-item label="使用时间" prop="use_time">
                    <el-input v-model="formData.use_time" clearable placeholder="请输入使用时间" />
                </el-form-item>
                <el-form-item label="是否上架:1=是,0=否" prop="on_shelf">
                    <el-radio-group v-model="formData.on_shelf" placeholder="请选择是否上架:1=是,0=否">
                        <el-radio v-for="(item, index) in dictData.on_shelf" :key="index" :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="formData.sort" clearable placeholder="请输入排序" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="boomDevicePriceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomDevicePriceAdd, apiBoomDevicePriceEdit, apiBoomDevicePriceDetail } from '@/api/device_price'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑设备价格表' : '新增设备价格表'
})

// 表单数据
const formData = reactive({
    id: '',
    device_id: '',
    icon: '',
    title: '',
    subhead: '',
    price: '',
    use_time: '',
    on_shelf: '',
    sort: '',
})


// 表单验证
const formRules = reactive<any>({
    device_id: [{
        required: true,
        message: '请选择设备ID',
        trigger: ['blur']
    }],
    title: [{
        required: true,
        message: '请输入标题',
        trigger: ['blur']
    }],
    price: [{
        required: true,
        message: '请输入价格',
        trigger: ['blur']
    }],
    use_time: [{
        required: true,
        message: '请输入使用时间',
        trigger: ['blur']
    }],
    on_shelf: [{
        required: true,
        message: '请选择是否上架:1=是,0=否',
        trigger: ['blur']
    }],
    sort: [{
        required: true,
        message: '请输入排序',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomDevicePriceDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiBoomDevicePriceEdit(data)
        : await apiBoomDevicePriceAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
